package com.lebang.medicalInquiry.service;




import com.lebang.medicalInquiry.mapper.ConfigMapper;
import com.lebang.medicalInquiry.model.po.Config;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: zpc
 * @date:
 */
@Service
public class ConfigService {

    @Resource
    private ConfigMapper configMapper;


    /**
     * 根据code获取配置信息
     *
     * @param code
     * @return
     */
    public String getByCode(Integer code) {
        Config config = configMapper.selectByCode(code);
        if(config==null){
            return "";
        }
        return config.getValue();
    }

    /**
     * @param code
     * @return
     */
    public boolean updateConfig(Config code) {
        try {
            Config config = configMapper.selectByCode(code.getCode());
            Config temp = new Config();
            temp.setValue(code.getValue());
            temp.setUpdatedTime(LocalDateTime.now());
            if (config != null) {
                temp.setId(config.getId());
                configMapper.updateByPrimaryKeySelective(temp);
            } else {
                temp.setCreatedTime(LocalDateTime.now());
                temp.setState(1);
                temp.setCode(code.getCode());
                configMapper.insertSelective(temp);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public Map<String, Object> getAll(Integer page, Integer limit) {
        HashMap<String, Object> map = new HashMap<>();
        List<Config> list = configMapper.selectAll(page, limit);
        int count = configMapper.count();
        map.put("list", list);
        map.put("count", count);
        return map;


    }

    public void add(Config config) {
        config.setCreatedTime(LocalDateTime.now());
        config.setUpdatedTime(LocalDateTime.now());
        configMapper.insert(config);
    }

    public void update(Config config) {
        config.setUpdatedTime(LocalDateTime.now());
        configMapper.updateByPrimaryKeySelective(config);
    }

    public void deleteById(Integer id) {
        configMapper.deleteByPrimaryKey(id);
    }
}

